class_name PlayerStateMoving
extends PlayerState


#func _ready() -> void:
	#ball.height_velocity = 0


func _process(_delta: float) -> void:
	if player.control_scheme == player.ControlScheme.CPU:
		ai_behavior.process_ai()
	else:
		handle_human_mivement()
	player.set_movement_animation()
	player.set_heading()


func handle_human_mivement() -> void:
	var direction := KeyUtils.get_input_vector(player.control_scheme)
	player.velocity = direction * player.speed
	
	#玩家按了方向，旋转视锥体
	if player.velocity != Vector2.ZERO:
		teammate_detection_area.rotation = player.velocity.angle()
	
	
	if KeyUtils.is_action_just_pressed(player.control_scheme,KeyUtils.Action.PASS):
			if player.has_ball():
				transition_state(Player.State.PASSING)
			if can_teammate_pass_ball() and KeyUtils.is_action_just_pressed(player.control_scheme,KeyUtils.Action.PASS):
				ball.carrier.get_pass_request(player)
			else:
				player.swap_requested.emit(player)
	
	elif KeyUtils.is_action_just_pressed(player.control_scheme,KeyUtils.Action.SHOOT):
		
		if player.has_ball():
			transition_state(Player.State.PREPPING_SHOT)
		#球是可互动转态，那么我们根据按键射门尝试射门动作
		elif ball.can_air_interact() :
			if player.velocity == Vector2.ZERO:
				if is_facing_target_goal():
					transition_state(Player.State.VOLLEY_KICK)
				else:
					transition_state(Player.State.BICYCLE_KICK)
				
			else:
				transition_state(Player.State.HEADER)
		elif player.velocity != Vector2.ZERO:
			state_transition_requested.emit(Player.State.TACKLING)
	


func is_facing_target_goal() -> bool:
	var direction_to_target_goal := player.position.direction_to(target_goal.position)
	
	return player.heading.dot(direction_to_target_goal)>0
	
#除了守门员，都可以在移动状态中拿球
func can_carry_ball() -> bool:
	return player.role != Player.Role.GOALIE
	

#队友可以传球吗
func can_teammate_pass_ball() -> bool:
	return (ball.carrier!=null and ball.carrier.country == player.country
	 and ball.carrier.control_scheme == Player.ControlScheme.CPU)



func can_pass() -> bool:
	return true
	
